package com.ysd.lis.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkCalcBatch;
import com.ysd.lis.entity.WkCalcResult;
import com.ysd.lis.mapper.WkCalcBatchMapper;
import com.ysd.lis.mapper.WkCalcResultMapper;
import com.ysd.lis.request.BuildResultDataParams;
import com.ysd.lis.request.WkCalcBatchRequest;
import com.ysd.lis.service.SysRuleSetService;
import com.ysd.lis.service.WkCalcBatchService;
import com.ysd.lis.service.WkCalcResultService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Timestamp;
import java.util.List;

/**
 * @author LuoXS
 * @date 2024/4/16
 * version 1.0
 */
@Service
@Slf4j
public class WkCalcBatchServiceImpl extends ServiceImpl<WkCalcBatchMapper, WkCalcBatch> implements WkCalcBatchService {

    @Autowired
    WkCalcBatchMapper mapper;
    @Autowired
    SysRuleSetService sysRuleSetService;
    @Autowired
    WkCalcResultService wkCalcResultService;
    @Resource
    WkCalcResultMapper wkCalcResultMapper;


    @Override
    public Result pageList(WkCalcBatchRequest.PageParam pageParam) {
        Result result;
        try {
            LambdaQueryWrapper<WkCalcBatch> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            if (StringUtils.isNotBlank(pageParam.getSearchValue())) {
                lambdaQueryWrapper.and(
                        w -> w.like(WkCalcBatch::getBatchCode, pageParam.getSearchValue()).or().like(WkCalcBatch::getBatchName, pageParam.getSearchValue())
                );
            }
            lambdaQueryWrapper.eq(WkCalcBatch::getDelFlag, 0);
            List<WkCalcBatch> wkCalcBatches = mapper.selectList(lambdaQueryWrapper);
            result = Result.succ(wkCalcBatches);
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("查询失败！");
        }
        return result;
    }

    @Override
    public Result add(WkCalcBatchRequest.AddParams params) {
        Result result = Result.succ(1, "添加成功！", null);
        try {
            String calcBatch = sysRuleSetService.createRuleCodeList("calc_batch", 1).get(0);
            params.setBatchCode(calcBatch);
            List<String> dates = params.getDates();
            String s = dates.get(0);
            String s2 = dates.get(1);
            params.setStartDate(new Timestamp(DateUtil.parse(s, "yyyy-MM-dd").getTime()));
            params.setEndDate(new Timestamp(DateUtil.parse(s2, "yyyy-MM-dd").getTime()));
            boolean save = this.save(params);
            if (save) {
                wkCalcResultService.buildData(BuildResultDataParams.builder()
                        .dates(params.getDates())
                        .batchId(params.getId())
                        .batchCode(params.getBatchCode())
                        .build()
                );
            } else {
                result = Result.fail("添加失败！");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail("添加失败！");
        }
        return result;
    }

    @Override
    public Result updateDit(WkCalcBatchRequest.UpdateParams params) {
        List<String> dates = params.getDates();
        String s = dates.get(0);
        String s2 = dates.get(1);
        params.setStartDate(new Timestamp(DateUtil.parse(s, "yyyy-MM-dd").getTime()));
        params.setEndDate(new Timestamp(DateUtil.parse(s2, "yyyy-MM-dd").getTime()));
        boolean b = this.updateById(params);
        if (b) {
            LambdaQueryWrapper<WkCalcResult> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(WkCalcResult::getBatchId, params.getId());
            queryWrapper.eq(WkCalcResult::getBatchCode, params.getBatchCode());
            wkCalcResultMapper.delete(queryWrapper);
            //执行跑数
            wkCalcResultService.buildData(BuildResultDataParams.builder()
                    .dates(params.getDates())
                    .batchId(params.getId())
                    .batchCode(params.getBatchCode())
                    .build()
            );
            return Result.succ(1, "编辑成功！", null);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Override
    public Result delete(WkCalcBatch params) {
        Result result;
        try {
            int i = mapper.deleteById(params.getId());
            if (i > 0) {
                LambdaQueryWrapper<WkCalcResult> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(WkCalcResult::getBatchId, params.getId());
                queryWrapper.eq(WkCalcResult::getBatchCode, params.getBatchCode());
                wkCalcResultMapper.delete(queryWrapper);
                result = Result.succ(1, "删除成功", null);
            } else {
                result = Result.fail(400, "删除失败", "");
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.fail(400, "删除失败", "");
        }
        return result;
    }

}
